package com.base.scanner;


import android.text.TextUtils;

import androidx.annotation.Nullable;

public class GlobalAudioPlayerManager {

    /**
     * 全局播放器
     */
    private static AudioPlayer mGlobalAudioPlayer;

    public static void replaceGlobalAudioPlayer(AudioPlayer currentAudioPlayer) {
        if (mGlobalAudioPlayer != null) {
            mGlobalAudioPlayer.release();
        }
        mGlobalAudioPlayer = currentAudioPlayer;
    }

    public static void clearGlobalAudioPlayer() {
        if (mGlobalAudioPlayer != null) {
            mGlobalAudioPlayer.release();
            mGlobalAudioPlayer = null;
        }
    }

    public static void clearAndNotifyGlobalAudioPlayer() {
        if (mGlobalAudioPlayer != null) {
            mGlobalAudioPlayer.release();
            mGlobalAudioPlayer = null;
//            RouterDataManager.getInstance().notifyPreviewPlayerClear();
        }
    }

    public static boolean checkIsNull() {
        return mGlobalAudioPlayer == null;
    }

    @Nullable
    public static AudioPlayer getGlobalAudioPlayer() {
        return mGlobalAudioPlayer;
    }

    public static boolean isPlaying() {
        return mGlobalAudioPlayer != null && mGlobalAudioPlayer.isPlaying();
    }

    public static boolean isCompletion() {
        return mGlobalAudioPlayer != null && mGlobalAudioPlayer.isCompletion();
    }

    public static int getCurrentProgress() {
        return mGlobalAudioPlayer != null ? mGlobalAudioPlayer.getCurrentProgress() : 0;
    }

    /**
     * @param audioFilePath 当前的播放资源路径
     * @return 检查当前播放资源和全局播放器的播放资源是否一样
     */
    public static boolean checkSourceIsSame(String audioFilePath) {
        if (!TextUtils.isEmpty(audioFilePath)) {
            if (mGlobalAudioPlayer != null) {
                return audioFilePath.equals(mGlobalAudioPlayer.getCurrentPlayerUrl());
            }
        }
        return false;
    }

    public static boolean checkIsSamePlayer(AudioPlayer currentPlayer) {
        return currentPlayer != null && currentPlayer == mGlobalAudioPlayer;
    }

    public static boolean checkIsDiffAndReplaceGlobalPlayer(AudioPlayer currentPlayer) {
        if (!checkIsSamePlayer(currentPlayer)) {
            replaceGlobalAudioPlayer(currentPlayer);
            return true;
        }
        return false;
    }

    public static void playOrPause() {
        if (mGlobalAudioPlayer == null) {
            return;
        }
        if (mGlobalAudioPlayer.isPlaying()) {
            mGlobalAudioPlayer.pause();
        } else {
            mGlobalAudioPlayer.play();
        }
    }

    public static void addPlayCallBack(OnVoicePlayerCallBack playerCallBack) {
        if (!checkIsNull()) {
            mGlobalAudioPlayer.addPlayCallBack(playerCallBack);
        }
    }
    public static void removePlayCallBack(OnVoicePlayerCallBack playerCallBack) {
        if (!checkIsNull()) {
            mGlobalAudioPlayer.removePlayCallBack(playerCallBack);
        }
    }

    public static void seekTo(int progress) {
        if (!checkIsNull()) {
            mGlobalAudioPlayer.seekTo(progress);
        }
    }

    public static void onPause() {
        if (!checkIsNull()) {
            mGlobalAudioPlayer.onPause();
        }
    }
}
